package xy.pubsub.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class RedisMessageConfig {



    /**
     * 注入消息监听适配器
     * @param messageListener
     * @return {@link MessageListenerAdapter}
     * @date 2021/9/7 下午 03:54
     */
    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(MsgListener messageListener){
        return new MessageListenerAdapter(messageListener);
    }
    /**
     * 注入消息监听容器
     * @param redisConnectionFactory
     * @param messageListenerAdapter
     * @return {@link RedisMessageListenerContainer}
     * @throws
     * @author Bai
     * @date 2021/9/7 下午 03:54
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter){
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        //订阅订单支付成功主题
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic("pubsub:queue"));
        return redisMessageListenerContainer;
    }
}

